global with sharing class PopulateBatchedUsers{
    static String var = '';
    
    //Method invoked from 'retreiveTemplates' function of 'BatchDet_AftGen_SCon'
    WebService static String getStartEndDateTime(String batchId)
    {
        DataAccessControlUtil dacu = new DataAccessControlUtil();
        SObject bObj = new Batch__c();

        Map<String, Schema.SObjectField> fieldMap = dacu.getFieldMap(bObj);
        
        String[] bExpectedFields = new String[]{'Id','StartTime__c', 'EndTime__c'}; 
        Set<String> allowedROFields = dacu.getAccessibleFields(bObj, fieldMap, bExpectedFields);
        
        String constructSoql = '';
        if(!allowedROFields.isEmpty())
        {
            constructSoql = 'Select ';
            for(String roField: allowedROFields)
            {
                constructSoql += roField+', ';
            }
            constructSoql = constructSoql.substring(0, constructSoql.length() - 2);
            constructSoql += ' from Batch__c where id = :batchId';

            Batch__c[] batch = Database.query(constructSoql);//[Select StartTime__c, EndTime__c from Batch__c b where b.Id = :batchId];
            if(batch.size() >0)
                return batch[0].StartTime__c.format('MM/dd/yyyy hh:mm:ss a')+'__'+((batch[0].EndTime__c == null)? '' : batch[0].EndTime__c.format('MM/dd/yyyy hh:mm:ss a'));
    
            
        }       
        return null;
    }
    
    WebService static String populateRemoteAddress(String URL)
    {
        try
        {
            DataAccessControlUtil dacu = new DataAccessControlUtil();
            SObject riaObj = new Remote_IP_Address__c();
    
            Map<String, Schema.SObjectField> fieldMap = dacu.getFieldMap(riaObj);
            
            String[] riaExpectedFields = new String[]{'IPAddress__c'}; 
            Set<String> allowedROFields = dacu.getAccessibleFields(riaObj, fieldMap, riaExpectedFields);
            
            String constructSoql = '';
            if(!allowedROFields.isEmpty())
            {
                constructSoql = 'Select ';
                for(String roField: allowedROFields)
                {
                    constructSoql += roField+', ';
                }
                constructSoql = constructSoql.substring(0, constructSoql.length() - 2);
                constructSoql += ' from Remote_IP_Address__c ';
                
                if(constructSoql.contains('IPAddress__c'))
                {
                    Remote_IP_Address__c[] rip = Database.query(constructSoql);//[select IPAddress__c from Remote_IP_Address__c];
                    if(rip.size() > 0)
                    {

                        String message = dacu.isUpdateableObjectByUser(riaObj);
                        if(message.equalsIgnoreCase('success'))
                        {
                            //Note: Whenever a new Custom field has to be updated, add it to the below Set.
                            //Reason: The size of the below set will be compared with the size of the filtered set 
                            Set<String> fieldsToBeUpdatedList = new Set<String>{'IPAddress__c'};
                            
                            //Store the expected updated fields and their values
                            Map<String, Object> fieldsValMap = new Map<String, Object>();
                            fieldsValMap.put('IPAddress__c', URL);
                            
                            //Filter the above list to get the fields that can be updated by the User. 
                            Set<String> filteredList = dacu.getFilteredUpdateableFields(fieldMap, fieldsToBeUpdatedList);
                            
                            riaObj = rip[0];
                            
                            if(filteredList.size() == fieldsToBeUpdatedList.size())
                            {
                                for(String s: filteredList)
                                {
                                    riaObj.put(s, fieldsValMap.get(s));
                                }
                                update riaObj;                              
                            }
                        }
                        else
                            return message;     
                    }
                    else
                    {

                        String message = dacu.isCreateableObjectByUser(riaObj);
                        if(message.equalsIgnoreCase('success'))
                        {
                            //Note: Whenever a new Custom field has to be created, add it to the below Set.
                            //Reason: The size of the below set will be compared with the size of the filtered set 
                            Set<String> fieldsToBeCreatedList = new Set<String>{'IPAddress__c'};
                            
                            //Store the expected created fields and their values
                            Map<String, Object> fieldsValMap = new Map<String, Object>();
                            fieldsValMap.put('IPAddress__c', URL);
                            
                            //Filter the above list to get the fields that can be created by the User. 
                            Set<String> filteredList = dacu.getFilteredCreateableFields(fieldMap, fieldsToBeCreatedList);
                            
                            riaObj = new Remote_IP_Address__c();
                            
                            if(filteredList.size() == fieldsToBeCreatedList.size())
                            {
                                for(String s: filteredList)
                                {
                                    riaObj.put(s, fieldsValMap.get(s));
                                }
                                insert riaObj;
                            }
                        }
                        else
                            return message;     
                    }
                }
            }           
        }
        catch(Exception e)
        {
            return e.getMessage();
        }
        return 'success';
    }//end of populateRemoteAddress()
    
    //This code will execute in Batch process
    Webservice static String updatePlanParticipants()
    {
        List<CallidusDataSource__c> dataSourceList = new List<CallidusDataSource__c>();
        List<SObject> sObjectList = new List<CallidusDataSource__c>();
        
        DataAccessControlUtil dacu = new DataAccessControlUtil();
        SObject cdsObj = new CallidusDataSource__c();

        Map<String, Schema.SObjectField> fieldMap = dacu.getFieldMap(cdsObj);

        //Note: Whenever a new Custom field has to be updated, add it to the below Set.
        //Reason: The size of the below set will be compared with the size of the filtered set 
        Set<String> cdsUpdatedFields = new Set<String>{'Name1__c','Name2__c','Name3__c','Name4__c','Name5__c','Name6__c','Name7__c','Name8__c','Name9__c','Name10__c','Name11__c','Name12__c','Name13__c','Name14__c','Name15__c','Name16__c','Name17__c','Name18__c','Name19__c','Name20__c','Name21__c','Name22__c','Name23__c','Name24__c','Name25__c','Name26__c','Name27__c','Name28__c','Name29__c','Name30__c','Value1__c','Value2__c','Value3__c','Value4__c','Value5__c','Value6__c','Value7__c','Value8__c','Value9__c','Value10__c','Value11__c','Value12__c','Value13__c','Value14__c','Value15__c','Value16__c','Value17__c','Value18__c','Value19__c','Value20__c','Value21__c','Value22__c','Value23__c','Value24__c','Value25__c','Value26__c','Value27__c','Value28__c','Value29__c','Value30__c','Name31__c','Name32__c','Name33__c','Name34__c','Name35__c','Name36__c','Name37__c','Name38__c','Name39__c','Name40__c','Name41__c','Name42__c','Name43__c','Name44__c','Name45__c','Name46__c','Name47__c','Name48__c','Name49__c','Name50__c','Name51__c','Name52__c','Name53__c','Name54__c','Value31__c','Value32__c','Value33__c','Value34__c','Value35__c','Value36__c','Value37__c','Value38__c','Value39__c','Value40__c','Value41__c','Value42__c','Value43__c','Value44__c','Value45__c','Value46__c','Value47__c','Value48__c','Value49__c','Value50__c','Value51__c','Value52__c','Value53__c','Value54__c'}; 
        Set<String> filteredFields = dacu.getFilteredUpdateableFields( fieldMap, cdsUpdatedFields);
        
        String constructSoql = '';
        
        //Enters only if the filteredList size is equivalent to the size of expected updateable fields List 
        if(filteredFields.size() == cdsUpdatedFields.size())
        {
            constructSoql = 'Select ';
            for(String roField: filteredFields)
            {
                constructSoql += roField+', ';
            }
            constructSoql = constructSoql.substring(0, constructSoql.length() - 2);
            constructSoql += ' from CallidusDataSource__c ';
            
            if(constructSoql.contains('Id'))
            {
                for(CallidusDataSource__c[] callidusUsers : Database.query(constructSoql))//[Select Id,Name1__c, Name2__c, Name3__c, Name4__c, Name5__c, Name6__c, Name7__c, Name8__c, Name9__c, Name10__c, Name11__c, Name12__c, Name13__c, Name14__c, Name15__c, Name16__c, Name17__c, Name18__c, Name19__c, Name20__c, Name21__c, Name22__c, Name23__c, Name24__c, Name25__c, Name26__c, Name27__c, Name28__c, Name29__c, Name30__c, Value1__c, Value2__c, Value3__c, Value4__c, Value5__c, Value6__c, Value7__c, Value8__c, Value9__c, Value10__c, Value11__c, Value12__c, Value13__c, Value14__c, Value15__c, Value16__c, Value17__c, Value18__c, Value19__c, Value20__c, Value21__c, Value22__c, Value23__c, Value24__c, Value25__c, Value26__c, Value27__c, Value28__c, Value29__c, Value30__c, Name31__c, Name32__c, Name33__c, Name34__c, Name35__c, Name36__c, Name37__c, Name38__c, Name39__c, Name40__c, Name41__c, Name42__c, Name43__c, Name44__c, Name45__c, Name46__c, Name47__c, Name48__c, Name49__c, Name50__c, Name51__c, Name52__c, Name53__c, Name54__c, Value31__c, Value32__c, Value33__c, Value34__c, Value35__c, Value36__c, Value37__c, Value38__c, Value39__c, Value40__c, Value41__c, Value42__c, Value43__c, Value44__c, Value45__c, Value46__c, Value47__c, Value48__c, Value49__c, Value50__c, Value51__c, Value52__c, Value53__c, Value54__c from CallidusDataSource__c])
                {        
                    for(CallidusDataSource__c user:callidusUsers)
                    {
                        user.Name1__c = user.Name2__c = user.Name3__c = user.Name4__c = user.Name5__c = user.Name6__c = user.Name7__c = user.Name8__c = user.Name9__c = user.Name10__c = user.Name11__c = user.Name12__c = user.Name13__c = user.Name14__c = user.Name15__c = user.Name16__c = user.Name17__c = user.Name18__c = user.Name19__c = user.Name20__c = user.Name21__c = user.Name22__c = user.Name23__c = user.Name24__c = user.Name25__c = user.Name26__c = user.Name27__c = user.Name28__c = user.Name29__c = user.Name30__c = user.Value1__c = user.Value2__c = user.Value3__c = user.Value4__c = user.Value5__c = user.Value6__c = user.Value7__c = user.Value8__c = user.Value9__c = user.Value10__c = user.Value11__c = user.Value12__c = user.Value13__c = user.Value14__c = user.Value15__c = user.Value16__c = user.Value17__c = user.Value18__c = user.Value19__c = user.Value20__c = user.Value21__c = user.Value22__c = user.Value23__c = user.Value24__c = user.Value25__c = user.Value26__c = user.Value27__c = user.Value28__c = user.Value29__c = user.Value30__c = user.Name31__c = user.Name32__c = user.Name33__c = user.Name34__c = user.Name35__c = user.Name36__c = user.Name37__c = user.Name38__c = user.Name39__c = user.Name40__c = user.Name41__c = user.Name42__c = user.Name43__c = user.Name44__c = user.Name45__c = user.Name46__c = user.Name47__c = user.Name48__c = user.Name49__c = user.Name50__c = user.Name51__c = user.Name52__c = user.Name53__c = user.Name54__c = user.Value31__c = user.Value32__c = user.Value33__c = user.Value34__c = user.Value35__c = user.Value36__c = user.Value37__c = user.Value38__c = user.Value39__c = user.Value40__c = user.Value41__c = user.Value42__c = user.Value43__c = user.Value44__c = user.Value45__c = user.Value46__c = user.Value47__c = user.Value48__c = user.Value49__c = user.Value50__c = user.Value51__c = user.Value52__c = user.Value53__c = user.Value54__c = '';
                         
                        dataSourceList.add(user);
                        if(dataSourceList.size() == 200)
                        {
                            update dataSourceList;
                            dataSourceList.clear();
                        }
        
                    }//end of inner for
                }//end of outer for     
            }
        }


        if(dataSourceList.size() > 0)
        {
            update dataSourceList;
            dataSourceList.clear();
        }
        return 'success';
        
    } 
    
    //This code executes in Batch process
    WebService static String populateObject(String[] UserIdList, String[] RoleIdList, String templateId, String bId)
    {
        String result = '';
        DataAccessControlUtil dacu = new DataAccessControlUtil();
        SObject bObj = new Batch__c();
        SObject buObj = new BatchedUser__c();
        
        //Enters only if User has permission to create Batch__c, BatchedUser__c and delete BatchedUser__c object
        if(dacu.isCreateableObjectByUser(bObj) == 'success' && dacu.isDeleteableObjectByUser(buObj) == 'success' && dacu.isCreateableObjectByUser(buObj) == 'success')
        {
            BatchedUser__c bu = null;
            Batch__c bch = null;
            String batchId = bId;
            BatchedUser__c[] bUserList = new List<BatchedUser__c>();
            Map<String, Schema.SObjectField> fieldMap = dacu.getFieldMap(buObj);
       
            if(bId != 'null')
            {
                String[] buExpectedFields = new String[]{'Id'}; 
                Set<String> allowedFields = dacu.getAccessibleFields(bObj, fieldMap, buExpectedFields);
                String constructSoql = '';
                if(!allowedFields.isEmpty())
                {
                    constructSoql = 'Select ';
                    for(String field: allowedFields)
                    {
                        constructSoql += field+', ';
                    }
                    constructSoql = constructSoql.substring(0, constructSoql.length() - 2);
                    constructSoql += ' from BatchedUser__c where BatchId__c = :bId';
                    for(BatchedUser__c[] bUsr : Database.query(constructSoql))//[SELECT Id from BatchedUser__c b where b.Batchid__c = :bId])
                        delete bUsr;
                }
                
            }
            else
            {
                bch = new Batch__c();
                insert bch;
                batchId = bch.Id;
            }
        
            Integer userSize = UserIdList.size();

            //Note: Whenever a new Custom field has to be created, add it to the below Set.
            //Reason: The size of the below set will be compared with the size of the filtered set 
            Set<String> fieldsToBeCreatedList = new Set<String>{'TemplateId__c','UserId__c','UserRoleId__c','Batchid__c'};
            
            //Filter the above list to get the fields that can be created by the User. 
            Set<String> filteredList = dacu.getFilteredCreateableFields(fieldMap, fieldsToBeCreatedList);
            
            //Enters only if the retrieved filteredList size is equal to actual createable fields list
            if(filteredList.size() == fieldsToBeCreatedList.size())
            {
                for(Integer i=0; i< userSize; i++)
                {
                    bu = new BatchedUser__c();
                    bu.TemplateId__c = templateId;
                    bu.UserId__c = UserIdList[i];
                    bu.UserRoleId__c = RoleIdList[i];
                    bu.Batchid__c = batchId;
                    bUserList.add(bu);
                    if(bUserList.size() == 200)
                    {
                        insert bUserList;
                        bUserList.clear();
                    }
                }
                if(bUserList.size() > 0)
                {    
                    insert bUserList;
                    bUserList.clear();
                }
            }
            return batchId;
        }
        return null;
    }


    WebService static String[] getSelectedUsers(String batchId, String templateId)
    {
        DataAccessControlUtil dacu = new DataAccessControlUtil();
        SObject bObj = new BatchedUser__c();

        Map<String, Schema.SObjectField> fieldMap = dacu.getFieldMap(bObj);
        
        String[] bExpectedFields = new String[]{'UserId__c', 'UserRoleId__c', 'TemplateId__c', 'User_Title__c'}; 
        Set<String> allowedROFields = dacu.getAccessibleFields(bObj, fieldMap, bExpectedFields);
        List<String> batchList = new List<String>();
        
        String constructSoql = '';
        
        String[] bUserArray = null;
        if(!allowedROFields.isEmpty())
        {
            constructSoql = 'Select ';
            for(String roField: allowedROFields)
            {
                constructSoql += roField+', ';
            }
            constructSoql = constructSoql.substring(0, constructSoql.length() - 2);
            constructSoql += ' from BatchedUser__c b where b.Batchid__c = :batchId limit 1000 ';


           BatchedUser__c[] bUser = Database.query(constructSoql);//[SELECT UserId__c, UserRoleId__c, TemplateId__c, User_Title__c from BatchedUser__c b where b.Batchid__c = :batchId limit 1000];
           bUserArray = new String[]{};

           Integer i = 0;
           if(bUser.size() > 0)
           {
               for(BatchedUser__c bU: bUser)
               {
                    CallidusDataSource__c[] dataSourceUserList = null;
                    if(bu != null)
                        dataSourceUserList = [SELECT FirstName__c, LastName__c, UserRoleName__c, User_Title__c FROM CallidusDataSource__c c where c.UserID__c = :bU.UserId__c  and (c.UserRoleId__c = :bU.UserRoleId__c or c.User_Title__c = :bU.User_Title__c)];
                    if(dataSourceUserList.size() > 0)
                    {
                        String userDetail = dataSourceUserList[0].FirstName__c+' '+dataSourceUserList[0].LastName__c+'__'+dataSourceUserList[0].UserRoleName__c+'__'+dataSourceUserList[0].User_Title__c;
                
                        bUserArray.add(userDetail);
                    
                        i++;
                    }
               }
            }
        }
        return bUserArray;
    }//end of getSelectedUsers

    WebService static String[] getBatchDetails(String templateId)
    {
        DataAccessControlUtil dacu = new DataAccessControlUtil();
        SObject bObj = new Batch__c();

        Map<String, Schema.SObjectField> fieldMap = dacu.getFieldMap(bObj);
        
        String[] bExpectedFields = new String[]{'Id', 'Name', 'Status__c', 'StartTime__c', 'EndTime__c', 'TotalContracts__c'}; 
        Set<String> allowedROFields = dacu.getAccessibleFields(bObj, fieldMap, bExpectedFields);
        List<String> batchList = new List<String>();
        
        String constructSoql = '';
        if(!allowedROFields.isEmpty())
        {
            constructSoql = 'Select ';
            for(String roField: allowedROFields)
            {
                constructSoql += roField+', ';
            }
            constructSoql = constructSoql.substring(0, constructSoql.length() - 2);
            constructSoql += ' from Batch__c b where b.TemplateId__c = :templateId and b.Status__c != \'INProgress\' limit 100 ';

           for(Batch__c[] batch : Database.query(constructSoql))//[SELECT Id, Name, Status__c, StartTime__c, EndTime__c, TotalContracts__c from Batch__c b where b.TemplateId__c = :templateId and b.Status__c != 'INProgress' limit 100])
           {
               for(Batch__c b: batch)
               {
                   String batchDetail = b.Id+'__'+b.Name+'__'+b.Status__c+'__'+b.StartTime__c.format('MM/dd/yyyy hh:mm:ss a')+'__'+((b.EndTime__c == null)? '' : b.EndTime__c.format('MM/dd/yyyy hh:mm:ss a'))+'__'+b.TotalContracts__c;
                   batchList.add(batchDetail);
                   if(batchList.size() == 100)
                        break;
               }
               if(batchList.size() == 100)
                    break;
           }
        }
       return batchList;
    }
    
    Webservice static String updatPlanDocumentDetails(ID scId, String status)
    {
        return 'success';
    }
    
    
    WebService static String insertMergeNames(String[] checkedNames, String templateName, String templateId, String payeeId, String effectiveDate)
    {
        DataAccessControlUtil dacu = new DataAccessControlUtil();
        SObject sObj = new TemplateDataMap__c();
        
        if(dacu.isCreateableObjectByUser(sObj) == 'success' && dacu.isCreateableObjectByUser(sObj) == 'success')
        {
            Map<String, Schema.SObjectField> fieldMap = dacu.getFieldMap(sObj);
    
            //Note: Whenever a new Custom field has to be updated or created, add it to the below Set.
            //Reason: The size of the below set will be compared with the size of the filtered set 
            Set<String> tdmFields = new Set<String>{'Name1__c','Name2__c','Name3__c','Name4__c','Name5__c','Name6__c','Name7__c','Name8__c','Name9__c','Name10__c','Name11__c','Name12__c','Name13__c','Name14__c','Name15__c','Name16__c','Name17__c','Name18__c','Name19__c','Name20__c','Name21__c','Name22__c','Name23__c','Name24__c','Name25__c','Name26__c','Name27__c','Name28__c','Name29__c','Name30__c','TemplateName__c','PayeeID__c','EffectiveDate__c'}; 
            Set<String> updateableFilteredFields = dacu.getFilteredUpdateableFields( fieldMap, tdmFields);
            Set<String> createableFilteredFields = dacu.getFilteredCreateableFields(fieldMap, tdmFields);
            if(updateableFilteredFields.size() == createableFilteredFields.size())
            {
                String constructSoql = '';
                if(!updateableFilteredFields.isEmpty())
                {
                    constructSoql = 'Select ';
                    for(String roField: updateableFilteredFields)
                    {
                        constructSoql += roField+', ';
                    }
                    constructSoql = constructSoql.substring(0, constructSoql.length() - 2);
                    constructSoql += ' from TemplateDataMap__c t where t.TemplateId__c = :templateId ';
                
                    TemplateDataMap__c[] tdm = Database.query(constructSoql);//[Select Name1__c, Name2__c, Name3__c, Name4__c, Name5__c, Name6__c, Name7__c, Name8__c, Name9__c, Name10__c, Name11__c, Name12__c, Name13__c, Name14__c, Name15__c, Name16__c, Name17__c, Name18__c, Name19__c, Name20__c, Name21__c, Name22__c, Name23__c, Name24__c, Name25__c, Name26__c, Name27__c, Name28__c, Name29__c, Name30__c,TemplateName__c, PayeeID__c, EffectiveDate__c from TemplateDataMap__c t where t.TemplateId__c = :templateId];
                    if(tdm.size() == 0)        
                    {
                        tdm = new TemplateDataMap__c[1];
                        tdm[0] = new TemplateDataMap__c(TemplateName__c = templateName, TemplateId__c = templateId, PayeeID__c = payeeId, EffectiveDate__c = Date.valueOf(effectiveDate));
                    }
                    Integer k=0;
                    Integer arraySize = checkedNames.size();
                    
                    tdm[0].PayeeID__c = payeeId;
                    
                    tdm[0].EffectiveDate__c = Date.valueOf(effectiveDate);
                    
                    tdm[0].Name1__c = checkedNames[k];
                    k++;
                    tdm[0].Name2__c = checkedNames[k];
                    k++;
                    tdm[0].Name3__c = checkedNames[k];
                    k++;
                    tdm[0].Name4__c = checkedNames[k];
                    k++;
                    tdm[0].Name5__c = checkedNames[k];
                    k++;
                    tdm[0].Name6__c = checkedNames[k];
                    k++;
                    tdm[0].Name7__c = checkedNames[k];
                    k++;
                    tdm[0].Name8__c = checkedNames[k];
                    k++;
                    tdm[0].Name9__c = checkedNames[k];
                    k++;
                        
                    if((k+1) <= arraySize) 
                    {
                        tdm[0].Name10__c = checkedNames[k];
                        k++;
                    }
                    else
                    {
                        tdm[0].Name10__c = '';
                    }
                    if((k+1) <= arraySize) 
                    {
                        tdm[0].Name11__c = checkedNames[k];
                        k++;
                    }
                    else
                    {
                        tdm[0].Name11__c = '';
                    }
            
                    if((k+1) <= arraySize) 
                    {
                        tdm[0].Name12__c = checkedNames[k];
                        k++;
                    }
                    else
                    {
                        tdm[0].Name12__c = '';
                    }
                    if((k+1) <= arraySize) 
                    {
                        tdm[0].Name13__c = checkedNames[k];
                        k++;
                    }
                    else
                    {
                        tdm[0].Name13__c = '';
                    }
                    if((k+1) <= arraySize) 
                    {
                        tdm[0].Name14__c = checkedNames[k];
                        k++;
                    }
                    else
                    {
                        tdm[0].Name14__c = '';
                    }
                    if((k+1) <= arraySize) 
                    {
                        tdm[0].Name15__c = checkedNames[k];
                        k++;
                    }
                    else
                    {
                        tdm[0].Name15__c = '';
                    }
                    if((k+1) <= arraySize) 
                    {
                        tdm[0].Name16__c = checkedNames[k];
                        k++;
                    }
                    else
                    {
                        tdm[0].Name16__c = '';
                    }
                    if((k+1) <= arraySize) 
                    {
                        tdm[0].Name17__c = checkedNames[k];
                        k++;
                    }
                    else
                    {
                        tdm[0].Name17__c = '';
                    }
                    if((k+1) <= arraySize) 
                    {
                        tdm[0].Name18__c = checkedNames[k];
                        k++;
                    }
                    else
                    {
                        tdm[0].Name18__c = '';
                    }
                    if((k+1) <= arraySize) 
                    {
                        tdm[0].Name19__c = checkedNames[k];
                        k++;
                    }
                    else
                    {
                        tdm[0].Name19__c = '';
                    }
                    if((k+1) <= arraySize) 
                    {
                        tdm[0].Name20__c = checkedNames[k];
                        k++;
                    }
                    else
                    {
                        tdm[0].Name20__c = '';
                    }
                    if((k+1) <= arraySize) 
                    {
                        tdm[0].Name21__c = checkedNames[k];
                        k++;
                    }
                    else
                    {
                        tdm[0].Name21__c = '';
                    }
                    if((k+1) <= arraySize) 
                    {
                        tdm[0].Name22__c = checkedNames[k];
                        k++;
                    }
                    else
                    {
                        tdm[0].Name22__c = '';
                    }
                    if((k+1) <= arraySize) 
                    {
                        tdm[0].Name23__c = checkedNames[k];
                        k++;
                    }
                    else
                    {
                        tdm[0].Name23__c = '';
                    }
                    if((k+1) <= arraySize) 
                    {
                        tdm[0].Name24__c = checkedNames[k];
                        k++;
                    }
                    else
                    {
                        tdm[0].Name24__c = '';
                    }
                    if((k+1) <= arraySize) 
                    {
                        tdm[0].Name25__c = checkedNames[k];
                        k++;
                    }
                    else
                    {
                        tdm[0].Name25__c = '';
                    }
                    if((k+1) <= arraySize) 
                    {
                        tdm[0].Name26__c = checkedNames[k];
                        k++;
                    }
                    else
                    {
                        tdm[0].Name26__c = '';
                    }
                    if((k+1) <= arraySize) 
                    {
                        tdm[0].Name27__c = checkedNames[k];
                        k++;
                    }
                    else
                    {
                        tdm[0].Name27__c = '';
                    }
                    if((k+1) <= arraySize) 
                    {
                        tdm[0].Name28__c = checkedNames[k];
                        k++;
                    }
                    else
                    {
                        tdm[0].Name28__c = '';
                    }
                    if((k+1) <= arraySize) 
                    {
                        tdm[0].Name29__c = checkedNames[k];
                        k++;
                    }
                    else
                    {
                        tdm[0].Name29__c = '';
                    }
                    if((k+1) <= arraySize) 
                    {
                        tdm[0].Name30__c = checkedNames[k];
                        k++;
                    }
                    else
                    {
                        tdm[0].Name30__c = '';
                    }
            
                    
                    upsert tdm;
                }
            }
        }
        return 'inserted successfully.';
    }
    
    
    WebService static String deleteBatch(String batchId)
    {
        DataAccessControlUtil dacu = new DataAccessControlUtil();
        SObject sObj = new Sales_Contract__c();
        
        if(dacu.isDeleteableObjectByUser(sObj) == 'success')
        {
            for(List<Sales_Contract__c> sc : [Select Id from Sales_Contract__c sc where sc.BatchID__c = :batchId])
            {
               delete sc;
            }
        }
        
        sObj = new Batch__c();
        if(dacu.isDeleteableObjectByUser(sObj) == 'success')
        {
            Batch__c[] batch = [Select Id from Batch__c b where b.Id = :batchId];
            if(batch.size() > 0)
                delete batch;
        }

        return 'Batch Deleted';
    }//end of deleteBatch(....) method
    
    WebService static String populateObject2(String[] UserIdList, String[] TitleList, String templateId, String bId)
    {
        String result = '';
        DataAccessControlUtil dacu = new DataAccessControlUtil();
        SObject bObj = new Batch__c();
        SObject buObj = new BatchedUser__c();
        
        String batchId = bId;
        //Enters only if User has permission to create Batch__c, BatchedUser__c and delete BatchedUser__c object
        if(dacu.isCreateableObjectByUser(bObj) == 'success' && dacu.isDeleteableObjectByUser(buObj) == 'success' && dacu.isCreateableObjectByUser(buObj) == 'success')
        {
            BatchedUser__c bu = null;
            Batch__c bch = null;
            BatchedUser__c[] bUserList = new List<BatchedUser__c>();
            Map<String, Schema.SObjectField> fieldMap = dacu.getFieldMap(buObj);
       
            if(bId != 'null')
            {
                String[] buExpectedFields = new String[]{'Id'}; 
                Set<String> allowedFields = dacu.getAccessibleFields(bObj, fieldMap, buExpectedFields);
                String constructSoql = '';
                if(!allowedFields.isEmpty())
                {
                    constructSoql = 'Select ';
                    for(String field: allowedFields)
                    {
                        constructSoql += field+', ';
                    }
                    constructSoql = constructSoql.substring(0, constructSoql.length() - 2);
                    constructSoql += ' from BatchedUser__c where BatchId__c = :bId';
                    for(BatchedUser__c[] bUsr : Database.query(constructSoql))//[SELECT Id from BatchedUser__c b where b.Batchid__c = :bId])
                        delete bUsr;
                }
                
            }
            else
            {
                bch = new Batch__c();
                insert bch;
                batchId = bch.Id;
            }
        
            Integer userSize = UserIdList.size();

            //Note: Whenever a new Custom field has to be created, add it to the below Set.
            //Reason: The size of the below set will be compared with the size of the filtered set 
            Set<String> fieldsToBeCreatedList = new Set<String>{'TemplateId__c','UserId__c','User_Title__c','Batchid__c'};
            
            //Filter the above list to get the fields that can be created by the User. 
            Set<String> filteredList = dacu.getFilteredCreateableFields(fieldMap, fieldsToBeCreatedList);
            
            //Enters only if the retrieved filteredList size is equal to actual createable fields list
            if(filteredList.size() == fieldsToBeCreatedList.size())
            {
                for(Integer i=0; i< userSize; i++)
                {
                    bu = new BatchedUser__c();
                    bu.TemplateId__c = templateId;
                    bu.UserId__c = UserIdList[i];
                    bu.User_Title__c = TitleList[i];
                    bu.Batchid__c = batchId;
                    bUserList.add(bu);
                    if(bUserList.size() == 200)
                    {
                        insert bUserList;
                        bUserList.clear();
                    }
                }
                if(bUserList.size() > 0)
                {    
                    insert bUserList;
                    bUserList.clear();
                }
            }
        }
        return batchId;
    }
    
    static testMethod void testPopulateBatchedUsers() 
    {
    	try
    	{
	        String[] userIdList = new String[1];
	        String[] roleIdList = new String[1];
	        User[] u = [select id from User limit 1];
	        UserRole[] ur = [select id from UserRole limit 1];
	        userIdList[0] = u[0].id;
	        roleIdList[0] = ur[0].id;
	        
	        Template__c temp = new Template__c(name = 'Sample Template');
	
	        Batch__c b = new Batch__c(StartTime__c = System.now(), EndTime__c = System.now());
	        Test.startTest();
	        insert temp;
	        Template__c[] tempT = [select name from Template__c where id = :temp.id];
	        if(tempT.size() > 0)
	        	system.assertEquals(temp.name, tempT[0].name);
	        insert b;
	        Batch__c[] batchT = [select name from Batch__c where id = :b.id];
	        if(batchT.size() > 0)
	        	system.assertEquals(b.id, batchT[0].id);
	        
	        String templateId = temp.id;
	        String batchId = b.id;
	        
	        PopulateBatchedUsers.populateObject(userIdList, roleIdList, templateId, batchId);
	        PopulateBatchedUsers.populateObject(userIdList, roleIdList, templateId, 'null');
	
	        PopulateBatchedUsers.populateObject2(userIdList, roleIdList, templateId, batchId);
	        PopulateBatchedUsers.populateObject2(userIdList, roleIdList, templateId, 'null');
	
	        PopulateBatchedUsers.getSelectedUsers(batchId, templateId);
	
	        PopulateBatchedUsers.getBatchDetails(templateId);
	        PopulateBatchedUsers.updatePlanParticipants();
	        
	        var = 'Published';
	        
	        system.assertEquals(null, PopulateBatchedUsers.getStartEndDateTime(''));
	        Sales_Contract__c sc = new Sales_Contract__c();
	        insert sc;
	        Sales_Contract__c[] scList = [select id, Name from Sales_Contract__c where id = :sc.id];
	        if(scList.size() > 0)
	        	system.assertEquals(scList[0].id,  sc.id);
	        	
	        updatPlanDocumentDetails(sc.id, 'Published');
	        
	        system.assertEquals('Batch Deleted', PopulateBatchedUsers.deleteBatch(batchId));
	        String[] mergeNames = new String[9];
	        mergeNames[0] = '';
	        mergeNames[1] = '';
	        mergeNames[2] = '';
	        mergeNames[3] = '';
	        mergeNames[4] = '';
	        mergeNames[5] = '';
	        mergeNames[6] = '';
	        mergeNames[7] = '';
	        mergeNames[8] = '';
	                
	        system.assertEquals('inserted successfully.', insertMergeNames(mergeNames, 'Test Jul 2',temp.Id,'' ,'2008-06-25'));
	
	        mergeNames = new String[30];
	        mergeNames[0] = '';
	        mergeNames[1] = '';
	        mergeNames[2] = '';
	        mergeNames[3] = '';
	        mergeNames[4] = '';
	        mergeNames[5] = '';
	        mergeNames[6] = '';
	        mergeNames[7] = '';
	        mergeNames[8] = '';
	        mergeNames[9] = '';
	        mergeNames[10] = '';
	        mergeNames[11] = '';
	        mergeNames[12] = '';
	        mergeNames[13] = '';
	        mergeNames[14] = '';
	        mergeNames[15] = '';
	        mergeNames[16] = '';
	        mergeNames[17] = '';
	        mergeNames[18] = '';
	        mergeNames[19] = '';
	        mergeNames[20] = '';
	        mergeNames[21] = '';
	        mergeNames[22] = '';
	        mergeNames[23] = '';
	        mergeNames[24] = '';
	        mergeNames[25] = '';
	        mergeNames[26] = '';
	        mergeNames[27] = '';
	        mergeNames[28] = '';
	        mergeNames[29] = '';
	                
	        system.assertEquals('inserted successfully.', insertMergeNames(mergeNames, 'Test Jul 2',temp.id,'' ,'2008-06-25'));
	        
	        Test.stopTest();
    	}
    	catch(Exception e){}
    }
    
    static testMethod void dacuTestCase() 
    {
        DataAccessControlUtil dacu = new DataAccessControlUtil();
        SObject sObj = new CallidusDataSOurce__c();
        Map<String, Schema.Sobjectfield> fieldsMap = dacu.getFieldMap(sObj);
        String[] expectedFieldMap = new String[]{'Id','Name'};
        Set<String> allowedFields = dacu.getAccessibleFields(sObj, fieldsMap, expectedFieldMap);
        Set<String> filteredList = dacu.getFilteredCreateableFields(fieldsMap, allowedFields);
        filteredList = dacu.getFilteredUpdateableFields(fieldsMap, allowedFields);
        system.assertEquals('success', dacu.isCreateableObjectByUser(sObj));
        system.assertEquals('success', dacu.isUpdateableObjectByUser(sObj));
        system.assertEquals('success', dacu.isDeleteableObjectByUser(sObj));
        dacu.pageMessage(sobj, 'create');
    }
    
}